<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
    under one or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information regarding copyright
    ownership. Camunda licenses this file to you under the Apache License,
    Version 2.0; you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
  
<mapper namespace="org.camunda.bpm.engine.impl.persistence.entity.IncidentEntity">

  <!-- INCIDENT DELETE -->

  <delete id="deleteIncident" parameterType="org.camunda.bpm.engine.impl.persistence.entity.IncidentEntity">
    delete from ${prefix}ACT_RU_INCIDENT where ID_ = #{id} and REV_ = #{revision}
  </delete>

  <!-- INCIDENT INSERT -->

  <insert id="insertIncident" parameterType="org.camunda.bpm.engine.impl.persistence.entity.IncidentEntity">
    insert into ${prefix}ACT_RU_INCIDENT (
      ID_, 
      INCIDENT_TIMESTAMP_, 
      INCIDENT_MSG_, 
      INCIDENT_TYPE_, 
      EXECUTION_ID_, 
      ACTIVITY_ID_, 
      FAILED_ACTIVITY_ID_, 
      PROC_INST_ID_, 
      PROC_DEF_ID_, 
      CAUSE_INCIDENT_ID_, 
      ROOT_CAUSE_INCIDENT_ID_, 
      CONFIGURATION_, 
      TENANT_ID_,
      JOB_DEF_ID_,
      ANNOTATION_,
      REV_)
    values (
      #{id ,jdbcType=VARCHAR},
      #{incidentTimestamp, jdbcType=TIMESTAMP},
      #{incidentMessage, jdbcType=VARCHAR},
      #{incidentType, jdbcType=VARCHAR},
      #{executionId ,jdbcType=VARCHAR},
      #{activityId ,jdbcType=VARCHAR},
      #{failedActivityId ,jdbcType=VARCHAR},
      #{processInstanceId ,jdbcType=VARCHAR},
      #{processDefinitionId ,jdbcType=VARCHAR},
      #{causeIncidentId ,jdbcType=VARCHAR},
      #{rootCauseIncidentId ,jdbcType=VARCHAR},
      #{configuration, jdbcType=VARCHAR},
      #{tenantId, jdbcType=VARCHAR},
      #{jobDefinitionId, jdbcType=VARCHAR},
      #{annotation, jdbcType=VARCHAR},
      1
    )
  </insert>

  <!-- INCIDENT RESULTMAP -->

  <resultMap id="incidentResultMap" type="org.camunda.bpm.engine.impl.persistence.entity.IncidentEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="incidentTimestamp" column="INCIDENT_TIMESTAMP_" jdbcType="TIMESTAMP" />
    <result property="incidentType" column="INCIDENT_TYPE_" jdbcType="VARCHAR" />
    <result property="incidentMessage" column="INCIDENT_MSG_" jdbcType="VARCHAR" />
    <result property="executionId" column="EXECUTION_ID_" jdbcType="VARCHAR" />
    <result property="activityId" column="ACTIVITY_ID_" jdbcType="VARCHAR" />
    <result property="failedActivityId" column="FAILED_ACTIVITY_ID_" jdbcType="VARCHAR" />
    <result property="processInstanceId" column="PROC_INST_ID_" jdbcType="VARCHAR" />
    <result property="processDefinitionId" column="PROC_DEF_ID_" jdbcType="VARCHAR" />
    <result property="causeIncidentId" column="CAUSE_INCIDENT_ID_" jdbcType="VARCHAR" />
    <result property="rootCauseIncidentId" column="ROOT_CAUSE_INCIDENT_ID_" jdbcType="VARCHAR" />
    <result property="configuration" column="CONFIGURATION_" jdbcType="VARCHAR" />
    <result property="tenantId" column="TENANT_ID_" jdbcType="VARCHAR" />
    <result property="jobDefinitionId" column="JOB_DEF_ID_" jdbcType="VARCHAR" />
    <result property="annotation" column="ANNOTATION_" jdbcType="VARCHAR" />
    <result property="revision" column="REV_" jdbcType="INTEGER" />
  </resultMap>
  
  <!-- INCIDENT SELECT -->

  <select id="selectIncident" parameterType="string" resultMap="incidentResultMap">
    select * from ${prefix}ACT_RU_INCIDENT where ID_ = #{id}
  </select>
  <select id="selectIncidentsByExecutionId" parameterType="string" resultMap="incidentResultMap">
    select *
    from ${prefix}ACT_RU_INCIDENT I
    where I.EXECUTION_ID_ = #{parameter}
  </select>
  
  <select id="selectIncidentsByProcessInstanceId" parameterType="string" resultMap="incidentResultMap">
    select *
    from ${prefix}ACT_RU_INCIDENT I
    where I.PROC_INST_ID_ = #{parameter}
  </select>

  <select id="selectIncidentsByConfiguration" resultMap="incidentResultMap">
    select *
    from ${prefix}ACT_RU_INCIDENT I
    where I.CONFIGURATION_ = #{parameter.configuration, jdbcType=VARCHAR}
    <if test="parameter.incidentType != null">
      and INCIDENT_TYPE_ = #{parameter.incidentType, jdbcType=VARCHAR}
    </if>
  </select>

  <select id="selectIncidentByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.IncidentQueryImpl" resultMap="incidentResultMap">
    <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.bindOrderBy"/>
    ${limitBefore}
    select ${distinct} RES.*
    ${limitBetween}
    <include refid="selectIncidentByQueryCriteriaSql"/>
    ${orderBy}
    ${limitAfter}
  </select>

  <select id="selectIncidentCountByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.IncidentQueryImpl" resultType="long">
    ${countDistinctBeforeStart} RES.ID_ ${countDistinctBeforeEnd}
    <include refid="selectIncidentByQueryCriteriaSql"/>
    ${countDistinctAfterEnd}
  </select>

  <sql id="selectIncidentByQueryCriteriaSql">
    from ${prefix}ACT_RU_INCIDENT RES

    <if test="(processDefinitionKeys != null &amp;&amp; processDefinitionKeys.length > 0)
        || (authCheck.shouldPerformAuthorizatioCheck &amp;&amp; authCheck.authUserId != null)">
      left join ${prefix}ACT_RE_PROCDEF PROCDEF
      on RES.PROC_DEF_ID_ = PROCDEF.ID_
    </if>

    <where>
      <if test="id != null">
        RES.ID_ = #{id}
      </if>
      <if test="incidentType != null">
        and RES.INCIDENT_TYPE_ = #{incidentType}
      </if>
      <if test="incidentMessage != null">
        and RES.INCIDENT_MSG_ = #{incidentMessage}
      </if>
      <if test="incidentMessageLike != null">
        and RES.INCIDENT_MSG_ like #{incidentMessageLike} ESCAPE ${escapeChar}
      </if>
      <if test="executionId != null">
        and RES.EXECUTION_ID_ = #{executionId}
      </if>
      <if test="incidentTimestampBefore != null">
        and RES.INCIDENT_TIMESTAMP_ &lt; #{incidentTimestampBefore}
      </if>
      <if test="incidentTimestampAfter != null">
        and RES.INCIDENT_TIMESTAMP_ &gt; #{incidentTimestampAfter}
      </if>
      <if test="activityId != null">
        and RES.ACTIVITY_ID_ = #{activityId}
      </if>
      <if test="failedActivityId != null">
        and RES.FAILED_ACTIVITY_ID_ = #{failedActivityId}
      </if>
      <if test="processInstanceId != null">
        and RES.PROC_INST_ID_ = #{processInstanceId}
      </if>
      <if test="processDefinitionId != null">
        and RES.PROC_DEF_ID_ = #{processDefinitionId}
      </if>
      <if test="processDefinitionKeys != null &amp;&amp; processDefinitionKeys.length > 0">
	      and PROCDEF.KEY_ in
        <foreach item="item" index="index" collection="processDefinitionKeys"
                 open="(" separator="," close=")">
          #{item}
        </foreach>
      </if>
      <if test="causeIncidentId != null">
        and RES.CAUSE_INCIDENT_ID_ = #{causeIncidentId}
      </if>
      <if test="rootCauseIncidentId != null">
        and RES.ROOT_CAUSE_INCIDENT_ID_ = #{rootCauseIncidentId}
      </if>
      <if test="configuration != null">
        and RES.CONFIGURATION_ = #{configuration}
      </if>
      
      <if test="tenantIds != null &amp;&amp; tenantIds.length > 0">
        and RES.TENANT_ID_ in
        <foreach item="tenantId" index="index" collection="tenantIds"
                 open="(" separator="," close=")">
          #{tenantId}
        </foreach>
      </if>
      <if test="jobDefinitionIds != null &amp;&amp; jobDefinitionIds.length > 0">
        and RES.JOB_DEF_ID_ in
        <foreach item="jobDefinitionId" index="index" collection="jobDefinitionIds"
                 open="(" separator="," close=")">
          #{jobDefinitionId}
        </foreach>
      </if>

      <if test="authCheck.shouldPerformAuthorizatioCheck &amp;&amp; authCheck.authUserId != null">
        AND
          ( CASE

            WHEN
              <!-- incident exists in context of a process instance -->
              RES.PROC_DEF_ID_ is not null
            THEN
              (
              <bind name="authUserId" value="authCheck.authUserId" />
              <bind name="authGroupIds" value="authCheck.authGroupIds" />
              <bind name="permissionChecks" value="authCheck.permissionChecks" />
              <bind name="revokeAuthorizationCheckEnabled" value="authCheck.revokeAuthorizationCheckEnabled" />
              <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.authorizationCheck" />)

            ELSE
              1
          END ) = 1
      </if>

      <include refid="org.camunda.bpm.engine.impl.persistence.entity.TenantEntity.queryTenantCheck" />

    </where>
  </sql>

  <update id="updateIncident" parameterType="org.camunda.bpm.engine.impl.persistence.entity.IncidentEntity">
    update ${prefix}ACT_RU_INCIDENT
    <set>
      REV_ = #{revisionNext, jdbcType=INTEGER},
      EXECUTION_ID_ = #{executionId, jdbcType=VARCHAR},
      PROC_DEF_ID_ = #{processDefinitionId, jdbcType=VARCHAR},
      ACTIVITY_ID_ = #{activityId, jdbcType=VARCHAR},
      FAILED_ACTIVITY_ID_ = #{failedActivityId, jdbcType=VARCHAR},
      JOB_DEF_ID_ = #{jobDefinitionId, jdbcType=VARCHAR},
      ANNOTATION_ = #{annotation, jdbcType=VARCHAR}
    </set>
    <where>
      ID_ = #{id, jdbcType=VARCHAR}
      and REV_ = #{revision, jdbcType=INTEGER}
    </where>
  </update>

</mapper>
